package com.zlk.algorithm.dataStructure.linkList03.pritice;

import com.zlk.algorithm.dataStructure.linkList03.SimpleNode;
import org.junit.Test;

/**
 * @program: algorithm
 * @ClassName FindMindNode
 * @description:
 * @author: slfang
 * @create: 2024-12-11 17:00
 * @Version 1.0
 **/
public class FindMindNode {

    @Test
    public void test(){
        SimpleNode test = null;
        test = new SimpleNode(0);
        test.next = new SimpleNode(1);
        test.next.next = new SimpleNode(2);
        test.next.next.next = new SimpleNode(3);
        test.next.next.next.next = new SimpleNode(4);
        test.next.next.next.next.next = new SimpleNode(5);
        test.next.next.next.next.next.next = new SimpleNode(6);
        test.next.next.next.next.next.next.next = new SimpleNode(7);
        test.next.next.next.next.next.next.next.next = new SimpleNode(8);
        test.next.next.next.next.next.next.next.next.next = new SimpleNode(9);
        System.out.println(getMidNode1(test).value);
        System.out.println(midOrDownMidNode(test).value);

    }

    /**
     * 输入链表头节点，奇数长度返回中点，偶数长度返回上中点
     *
     * 1 2 3 4 5 6 7 8 9 10
     *   s f
     *     s   f
     *       s     f
     *          s      f
     */
    private SimpleNode getMidNode1(SimpleNode head) {
        if(head==null||head.next==null||head.next.next==null){
            return head;
        }
        SimpleNode slow = head.next;
        SimpleNode fast = head.next.next;
        while (fast.next!=null&&fast.next.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    public  SimpleNode midOrDownMidNode(SimpleNode head) {
        if(head==null||head.next==null){
            if(head.next.next==null){
                return head.next;
            }else{
                return head;
            }
        }
        SimpleNode slow = head.next;
        SimpleNode fast = head.next.next;
        while (fast.next!=null&&fast.next.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        if(fast.next!=null){
            return slow.next;

        }else{
            return slow;
        }
    }
}
